﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZenInterface
{
    public partial class dataParserClass : Form
    {
        #region variables

        Properties.Settings settings;
        DataTable dataTable;
        int index = 0;

        String leftoverString;
        zenStatusClass zenStatus;

        DataTable overflowDataTable;
        int overflowIndex;
        bool overflowFlag;

        public bool status;
        public bool reset;

        #endregion

        public dataParserClass(Properties.Settings inSettings, DataTable inDataTable, zenStatusClass inZenStatus)
        {
            InitializeComponent();

            settings = inSettings;
            dataTable = inDataTable;
            zenStatus = inZenStatus;
            overflowDataTable = dataTable.Copy();
        }
        public bool parseIncommingData(String inputBuffer)
        {
            //variables
            char[] stringTokens = { '*' };
            char[] tokens = { ',' };
            String[] dataStrings;
            String[] data;
            int dataIndex = 0;
            DataRow tempRow;
            Int32 tempVariable;

            //if we had an overflow last time we need to copy the data from the overflow table to the output table
            if (overflowFlag)
            {
                for (index = 0; index < overflowIndex; index++)
                {
                    dataTable.Rows[index].ItemArray = overflowDataTable.Rows[index].ItemArray;
                }
            }

            //reset the overflow flag cause we should have been able to modify data when it was set
            overflowFlag = false;
            overflowIndex = 0;

            leftoverString += inputBuffer;

            tempRow = dataTable.Rows[index];

            //tempRow[9] = true;
            //parse out all the new lines of data
            dataStrings = leftoverString.Split(stringTokens, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < dataStrings.Length; i++)
            {



                //if we are at the last string in the String Array                
                if (overflowFlag == true)
                {
                    if (overflowIndex > 127)
                    {
                        //we overflowed the data, and if we let this go it would crash the system
                        //so reset it, and throw away data
                        overflowIndex = 0;
                    }
                    dataIndex = overflowIndex;
                    tempRow = overflowDataTable.Rows[dataIndex];

                }
                else
                {
                    dataIndex = index;
                    tempRow = dataTable.Rows[dataIndex];
                    //tempRow[4] = DateTime.Now;
                    //if (i != 0)
                    //{
                    //    tempRow[5] = false;
                    //}
                }

                if (i == (dataStrings.Length - 1))
                {
                    //check if it has the last character in the array
                    if (!dataStrings[i].Contains("x"))
                    {
                        if (dataStrings[i].Length == 0)
                        {
                            leftoverString = String.Empty;
                        }
                        else
                        {
                            leftoverString = dataStrings[i];
                            break;
                        }
                    }
                    else
                    {
                        leftoverString = String.Empty;
                    }
                }


                //parse the incomming data
                data = dataStrings[i].Split(tokens);


                //1st is channel 0
                if (data.Length > 0)
                {
                    Int32.TryParse(data[0], out (tempVariable));
                    //translate to signed
                    tempRow[0] = tempVariable - settings.userADCmidpoint;
                    
                }


                //2nd is channel 1
                if (data.Length > 1)
                {
                    int.TryParse(data[1], out tempVariable);
                    //translate to signed
                    tempRow[1] = tempVariable - settings.userADCmidpoint;
                }


                //3rd is channel 2
                if (data.Length > 2)
                {
                    int.TryParse(data[2], out tempVariable);
                    //translate to signed
                    tempRow[2] = tempVariable - settings.userADCmidpoint;
                }


                //4th is channel 3
                if (data.Length > 3)
                {
                    int.TryParse(data[3], out tempVariable);
                    //translate to signed
                    tempRow[3] = tempVariable - settings.userADCmidpoint;
                }

                //if there is a 5th then we have a timing signal
                if (data.Length > 4)
                {
                    tempRow[4] = data[4];
                }

                //if there is a 5th then we have a timing signal
                if (data.Length > 5)
                {
                    tempRow[5] = data[5];
                }

                //convert to integer, and analyze by boolean

                //1 = button 0
                //button[0] = ((temp & 1) != 0);
                //2 = button 1
                //button[1] = ((temp & 2) != 0);
                //4 = button 2
                //button[2] = ((temp & 4) != 0);
                //8 = button 3
                //button[3] = ((temp & 8) != 0);
                //16 = LED 0
                //LED[0] = ((temp & 16) != 0);
                //32 = LED 1
                //LED[1] = ((temp & 32) != 0);
                //64 = LED 2
                //LED[2] = ((temp & 64) != 0);
                //128 = LED 3
                //LED[3] = ((temp & 128) != 0);
                //256 = status
                //status = ((temp & 256) != 0);
                //512 = reset
                //reset = ((temp & 512) != 0);
                //1024 = ??
                //2056 = ??
                //4096 = ??
                //8192 = ??

                //are we done with this group of data?
                if (overflowFlag == true)
                {
                    overflowIndex++;
                }
                else
                {
                    index++;
                }

                if (index == settings.userDataBufferSize)
                {
                    overflowFlag = true;
                }
            }

            if (overflowFlag == true)
            {
                return true;
            }
            return false;
        }
        public void parseStatus(String statusBuffer)
        {
            //if we have a status string, we wait for the next string for the data
        }

        private void dataParserClass_FormClosing(object sender, FormClosingEventArgs e)
        {
            //we really don't want this thing to close, we just want it to hide
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                this.Hide();
            }
        }

        private void buttonSaveConfiguration_Click(object sender, EventArgs e)
        {

        }
    }
}
